package com.dsp.web;

import java.math.BigDecimal; 
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.hibernate.criterion.DetachedCriteria;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.core.cache.CacheInstance;
import com.core.util.DspUtil;
import com.core.util.Pagination;
import com.core.util.PinYin;
import com.dsp.bean.Company;
import com.dsp.bean.Employee;
import com.dsp.bean.Message;
import com.dsp.services.EmployeeService;

@Controller("EmployeesController")
public class EmployeesController extends DspController<Employee>{
	
	public static String[]  QueryColumn = {"company","empname","cMethod","hukou","isInsure","isProvident"};
	public static String[]  QueryType   = {"Long","String","Integer","Integer","Integer","Integer"};
	
	public EmployeesController() {
		super(Employee.class);
	}

	Logger logger = Logger.getLogger(DspUtil.Log4J);

	@Autowired
	private EmployeeService employeeService;

	@RequestMapping(value = "/employees", method = RequestMethod.GET)
	public ModelAndView list() {
		ModelAndView mav = new ModelAndView();
		mav.setViewName("employee/list");
		mav.addObject("path", "employee/list.jsp");
		mav.addObject("current", "employees");
		return mav;
	}
	/**
	 * 社保画面
	 * @return
	 */
	@RequestMapping(value = "/employees_page1",method = RequestMethod.GET)
	public ModelAndView page1(){
		ModelAndView mav = new ModelAndView();
		mav.setViewName("employee/page1");
		mav.addObject("path", "employee/page1.jsp");
		return mav;
	}
	/**
	 * 公积金画面
	 * @return
	 */
	@RequestMapping(value = "/employees_page2",method = RequestMethod.GET)
	public ModelAndView page2(){
		ModelAndView mav = new ModelAndView();
		mav.setViewName("employee/page2");
		mav.addObject("path", "employee/page2.jsp");
		return mav;
	}

	@RequestMapping(value = "/employees_list")
	@ResponseBody
	public Pagination<Employee> index(HttpServletRequest request, HttpServletResponse response) throws Exception{
		super.initHttp(request, response);
		DetachedCriteria dc = DetachedCriteria.forClass(Employee.class);
		super.getPageParameters(dc, QueryColumn	, QueryType);
		Pagination<Employee> p = this.employeeService.search(dc, (page - 1) * rp, rp);
		p.setPage(page);
		return p;
	}

	@RequestMapping(value = "/employees_create", method = RequestMethod.POST)
	@ResponseBody
	public Message create(@RequestBody Employee employee) {
		Message message = new Message();
		logger.info("正在创建员工信息");
		try {
			employee.setPyCode(new PinYin().toPinYin(employee.getEmpname(), true, false));
			Company com = CacheInstance.getInstance().getCompanyMap().get(employee.getCompany().toString());
			if (employee.getId() != null) {
				this.employeeService.update(employee);
			} else {
				if (employee.getIsInsure() == null) employee.setIsInsure(0);
				if (employee.getIsProvident() == null) employee.setIsProvident(0);
				employee.setInsureBase(com.getInsureBase()); //新增资料时，默认设置社保基数为公司社保基数
				employee.setProvidentBase(com.getProvidentBase());//新增资料时，默认设置公积金基数为公司公积金基数
				this.employeeService.save(employee);
			}
		} catch (Exception se) {
			super.initMessage(message , se.getLocalizedMessage());
			se.getStackTrace();
		}
		logger.info("完成创建员工信息");
		return message;
	}
	
	@RequestMapping(value = "/employees_remove" , method = RequestMethod.POST)
	@ResponseBody
	public Message removeOne(HttpServletRequest request, HttpServletResponse response){
		super.initHttp(request, response);
		Message message = new Message();
		Long id = null;
		String insure = request.getParameter("insure");
		String isInsure = "1".equalsIgnoreCase(insure) ? 
					request.getParameter("isInsure") : request.getParameter("isProvident");//0未加保;1加保;2退保
		if(!"1".equalsIgnoreCase(isInsure)){
			super.initMessage(message,"只能操作已加保的资料");
			return message;
		}
		try {
			id = Long.valueOf(request.getParameter("id"));
			Employee employee = this.employeeService.findById(id);
			if("1".equalsIgnoreCase(insure)){ //社保
				if(employee.getIsInsure() != 1){
					super.initMessage(message,"只能操作已加保的资料");
					return message;
				}
				employee.setIsInsure(2);
			} else if ("2".equalsIgnoreCase(insure)){ //公积金
				if(employee.getIsProvident() != 1){
					super.initMessage(message,"只能操作已加保的资料");
					return message;
				}
				employee.setIsProvident(2);
			} else {
				super.initMessage(message,"未授权的操作");
				return message;
			}
			this.employeeService.update(employee);
		} catch (Exception e){
			super.initMessage(message,e.getLocalizedMessage());
		}
		return message;
	
	}
	
	/**
	 * 修改是否加保/是否加公积金
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping(value = "/employees_one", method = RequestMethod.POST)
	@ResponseBody
	public Message updateOne(HttpServletRequest request, HttpServletResponse response) {
		super.initHttp(request, response);
		Message message = new Message();
		Long id = null;
		String insure = request.getParameter("insure");
		String isInsure = "1".equalsIgnoreCase(insure) ? 
					request.getParameter("isInsure") : request.getParameter("isProvident");//0未加保;1加保;2退保
		String baseMoney = "1".equalsIgnoreCase(insure) ? //基数 
					request.getParameter("insureBase") :request.getParameter("providentBase");
		baseMoney = baseMoney == null || "".equals(baseMoney)  ? "0" : baseMoney;
		if(!"0".equalsIgnoreCase(isInsure)){
			super.initMessage(message,"只能操作未加保的资料");
			return message;
		}
		try {
			id = Long.valueOf(request.getParameter("id"));
			Employee employee = this.employeeService.findById(id);
			if("1".equalsIgnoreCase(insure)){ //社保
				employee.setIsInsure(1);
				employee.setInsureBase(new BigDecimal(baseMoney));
			} else if ("2".equalsIgnoreCase(insure)){ //公积金
				employee.setIsProvident(1);
				employee.setProvidentBase(new BigDecimal(baseMoney));
			} else {
				super.initMessage(message,"未授权的操作");
				return message;
			}
			this.employeeService.update(employee);
		} catch (Exception e){
			super.initMessage(message,"e.getLocalizedMessage()");
		}
		return message;
	}

	@RequestMapping(value = "/employees_edit", method = RequestMethod.POST)
	@ResponseBody
	public Employee edit(@RequestBody Employee _employee) {
		Employee employee = this.employeeService.findById(_employee.getId());
		return employee;
	}

	@RequestMapping(value = "/employees_delete", method = RequestMethod.POST)
	@ResponseBody
	public Message delete(@RequestBody Employee employee) {
		Message message = new Message();
		String success = this.employeeService.delete(employee);
		message.setSuccess(Boolean.valueOf(success));
		return message;
	}

	/**
	 * @return the employeeService
	 */
	public EmployeeService getEmployeeService() {
		return employeeService;
	}

	/**
	 * @param employeeService
	 *            the employeeService to set
	 */
	public void setEmployeeService(EmployeeService employeeService) {
		this.employeeService = employeeService;
	}

}
